<?php
tsload(APPS_PATH.'/admin/Lib/Action/AdministratorAction.class.php');

class CoachAction extends AdministratorAction{
	/**
	 * 教练状态 
	 *  0 申请中
	 *  1审核通过
	 *  -1 被禁用
	 *  -2 不通过
	 */
	public function _initialize() {
		$this->pageTitle['index'] = '所有教练';
		$this->pageTitle['banCoach'] = '禁用列表';
		$this->pageTitle['coachApply'] = '教练申请审核';
		$this->pageTitle['unpassApply'] = '教练申请审核';
		$this->pageTitle['classList'] = '教练课程列表';
		$this->pageTitle['addCoach'] = '添加教练';
	
		parent::_initialize();
	}
	//所有教练界面
	public function index(){
		$_REQUEST['tabHash'] = 'index';
		// 初始化约单列表管理菜单
		$this->_initCoachListAdminMenu('index');
		$map["apply_status"] = 1; 
		$listData = $this->_getCoachList('20', $map, 'index');
		//dump($listData);exit;
		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		
		$this->displayList($listData);
	}
	
	public function banCoach(){
		$_REQUEST['tabHash'] = 'banCoach';
		// 初始化约单列表管理菜单
		$this->_initCoachListAdminMenu('banCoach');
		$map["apply_status"] = -1;
		$listData = $this->_getCoachList('20', $map, 'banCoach');
		//dump($listData);exit;
		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		
		$this->displayList($listData);
	}
	//教练申请审核界面
	public function coachApply(){
		$_REQUEST['tabHash'] = 'coachApply';
		// 初始化约单列表管理菜单
		$this->_initCoachListAdminMenu('coachApply');
		$map["apply_status"] = 0;
		$listData = $this->_getCoachList('20', $map, 'coachApply');
		
		//$this->pageButton[] = array('title'=>'设备搜索','onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		
		$this->displayList($listData);
	}
	//教练申请不通过列表
	public function unpassApply(){
		$_REQUEST['tabHash'] = 'unpassApply';
		// 初始化约单列表管理菜单
		$this->_initCoachListAdminMenu('unpassApply');
		$map["apply_status"] = -2;
		$listData = $this->_getCoachList('20', $map, 'unpassApply');
	
		//$this->pageButton[] = array('title'=>'设备搜索','onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>"批量删除",'onclick'=>"admin.DelCoachApplys()");
		$this->displayList($listData);
	}
	//课程列表
	public function classList(){
		$_REQUEST['tabHash'] = 'classList';
		// 初始化约单列表管理菜单
		$this->_initCoachListAdminMenu('classList');
		
		$listData = $this->_getCoachList('20', $map, 'classList');
		$this->opt["status"] = array('0'=>"进行中",'1'=>"已结课");
		//dump($listData);exit;
		//$this->pageButton[] = array('title'=>'设备搜索','onclick'=>"admin.fold('search_form')");
		$this->pageButton[] = array('title'=>L('PUBLIC_SEARCH_USER'),'onclick'=>"admin.fold('search_form')");
		
		$this->displayList($listData);
	}
	//课程展示界面
	private function _initCoachListAdminMenu($type) {
		// tab选项
		
			//所有约单栏目
			$this->pageTab[] = array('title'=>"所有教练",'tabHash'=>'index','url'=>U('admin/Coach/index'));
			$this->pageTab[] = array('title'=>"禁用列表",'tabHash'=>'banCoach','url'=>U('admin/Coach/banCoach'));
			$this->pageTab[] = array('title'=>"教练申请",'tabHash'=>'coachApply','url'=>U('admin/Coach/coachApply'));
			$this->pageTab[] = array('title'=>"不通过申请",'tabHash'=>'unpassApply','url'=>U('admin/Coach/unpassApply'));
			$this->pageTab[] = array('title'=>"教练课程列表",'tabHash'=>'classList','url'=>U('admin/Coach/classList'));
			$this->pageTab[] = array('title'=>"添加教练",'tabHash'=>'addCoach','url'=>U('admin/Coach/addCoach'));
			
	
	
		// 搜索选项的key值
		// $this->searchKey = array('uid','uname','email','sex','department','user_group',array('ctime','ctime1'));
	
	
		// 列表key值 DOACTION表示操作
		switch(strtolower($type)) {
			case 'index':
			case "bancoach":
			case "unpassapply":
			case 'coachapply':
				$this->searchKey = array('uid',"realname","level","sex",array("birth","birth1"),"sport",array("apply_time","apply_time1"));
				$this->pageKeyList = array('id','uid',"realname","level","sex","birth","sport","photo","diploma_photo","apply_status","apply_time",'DOACTION');
				break;
			case 'classlist':
				$this->searchKey = array('oid','title','content','coach_id','people_limit','peoplo_current',"start_time","status");
				$this->pageKeyList = array('oid','title','content','coach_id','people_limit','peoplo_current',"start_time","status",'DOACTION');
				break;
			
		}
		$this->opt["sex"] = array("2"=>"女",1=>"男");
		$this->opt["level"] = array("0"=>"初级",1=>"中级",2=>"高级");
		/*		if(!empty($_POST['_parent_dept_id'])) {
		 $this->onload[] = "admin.departDefault('".implode(',', $_POST['_parent_dept_id'])."','form_user_department')";
		}*/
	}
	
	/**
	 * 解析约单列表数据
	 * @param integer $limit 结果集数目，默认为20
	 * @param array $map 查询条件
	 * @param string $type 格式化数据类型，index、pending、dellist
	 * @return array 解析后的用户列表数据
	 */
	private function _getCoachList($limit = 20, $map = array(), $type = 'index') {
		
		if($type == "index"){
			// 设置列表主键
			$this->_listpk = 'id';
			// 取用户列表
			$listData = model('Coach')->getCoathList($limit,$map);
			//dump($listData);exit;
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['apply_time'] = date('Y-m-d H:i:s',$listData['data'][$k]['apply_time']);
				$listData['data'][$k]['apply_status'] = $listData['data'][$k]['apply_status']==0?"申请中":"已通过";
				$listData['data'][$k]['sex'] = $listData['data'][$k]['sex']==0?"女":"男";
				$listData['data'][$k]['level'] = $listData['data'][$k]['level']==0?"初级":($listData['data'][$k]['level']==1?"中级":"高级");
				$uid = $listData['data'][$k]['uid'];
				//$listData['data'][$k]['uid'] = M("User")->where("uid = ".$listData['data'][$k]['uid'])->find()["uname"];
				//图片处理	

				$image = D('attach')->where('attach_id='.$listData['data'][$k]['photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['photo'] = getImageUrl($image_url);
				$listData['data'][$k]['photo'] = '<div class = "diploma" style="text-align:center;"><div class="targets"><img src="'.$listData['data'][$k]['photo'].'" width=150 height=100 /></div></div>';
					
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['diploma_photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['diploma_photo'] = getImageUrl($image_url);
				$listData['data'][$k]['diploma_photo'] = '<div class = "diploma" style="text-align:center;"><div class="targets"><img src="'.$listData['data'][$k]['diploma_photo'].'" width=150 height=100 /></div></div>';
				
				$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Coach/addCalssToCoach',array('id'=>$v['id'],"uid"=>$uid)).'">'."添加课程".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="'.U('admin/Coach/editCoach',array('id'=>$v['id'])).'">'.L('PUBLIC_EDIT').'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.banOneCoach(\''.$v['id'].'\')">'."禁用".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.trueDelCoach(\''.$v['id'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
					
			}
		}elseif ($type == "banCoach"){
			// 设置列表主键
			$this->_listpk = 'id';
			// 取用户列表
			$listData = model('Coach')->getCoathList($limit,$map);
			//dump($listData);exit;
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['apply_time'] = date('Y-m-d H:i:s',$listData['data'][$k]['apply_time']);
				$listData['data'][$k]['apply_status'] = $listData['data'][$k]['apply_status']==0?"申请中":"被禁用";
				$listData['data'][$k]['sex'] = $listData['data'][$k]['sex']==0?"女":"男";
				$uid = $listData['data'][$k]['uid'];
				//$listData['data'][$k]['uid'] = M("User")->where("uid = ".$listData['data'][$k]['uid'])->find()["uname"];
				//图片处理
			
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['photo'] = getImageUrl($image_url);
				$listData['data'][$k]['photo'] = "<img style='height:100px;' src='".$listData['data'][$k]['photo']."'"." />";
					
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['diploma_photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['diploma_photo'] = getImageUrl($image_url);
				$listData['data'][$k]['diploma_photo'] = "<img style='height:100px;' src='".$listData['data'][$k]['diploma_photo']."'"." />";
			
				
				$listData['data'][$k]['DOACTION'] .= '<a href="'.U('admin/Coach/editCoach',array('id'=>$v['id'])).'">'.L('PUBLIC_EDIT').'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.recoverCoach(\''.$v['id'].'\')">'."复原".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.delCoach(\''.$v['id'].'\')">'."删除资格".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.trueDelCoach(\''.$v['id'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			}
		}elseif($type == "coachApply"){
			$this->_listpk = 'id';
			// 取用户列表
			$listData = model('Coach')->getCoathList($limit,$map);
			//dump($listData);exit;
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['apply_time'] = date('Y-m-d H:i:s',$listData['data'][$k]['apply_time']);
				$listData['data'][$k]['apply_status'] = $listData['data'][$k]['apply_status']==0?"申请中":"已通过";
				$listData['data'][$k]['sex'] = $listData['data'][$k]['sex']==0?"女":"男";
				//$listData['data'][$k]['uid'] = M("User")->where("uid = ".$listData['data'][$k]['uid'])->find()["uname"];
				
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['photo'] = getImageUrl($image_url);
				$listData['data'][$k]['photo'] = '<div class = "diploma" style="text-align:center;"><div class="targets"><img src="'.$listData['data'][$k]['photo'].'" width=150 height=100 /></div></div>';
					
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['diploma_photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['diploma_photo'] = getImageUrl($image_url);
				$listData['data'][$k]['diploma_photo'] = '<div class = "diploma" style="text-align:center;"><div class="targets"><img src="'.$listData['data'][$k]['diploma_photo'].'" width=150 height=100 /></div></div>';
				
				$listData['data'][$k]['DOACTION'] = '<a href="javascript:void(0)" onclick="admin.passCoach(\''.$v['id'].'\')">'."通过".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.unpassCoach(\''.$v['id'].'\')">'."不通过".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.DelCoachApply(\''.$v['id'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			}
		}elseif($type == "unpassApply"){
			$this->_listpk = 'id';
			// 取用户列表
			$listData = model('Coach')->getCoathList($limit,$map);
			//dump($listData);exit;
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['apply_time'] = date('Y-m-d H:i:s',$listData['data'][$k]['apply_time']);
				$listData['data'][$k]['apply_status'] = $listData['data'][$k]['apply_status']==-2?"不通过":"申请中";
				$listData['data'][$k]['sex'] = $listData['data'][$k]['sex']==0?"女":"男";
				//$listData['data'][$k]['uid'] = M("User")->where("uid = ".$listData['data'][$k]['uid'])->find()["uname"];
				
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['photo'] = getImageUrl($image_url);
				$listData['data'][$k]['photo'] = "<img style='height:100px;' src='".$listData['data'][$k]['photo']."'"." />";
					
				$image = D('attach')->where('attach_id='.$listData['data'][$k]['diploma_photo'])->find();
				$image_url = $image['save_path'].$image['save_name'];
				$listData['data'][$k]['diploma_photo'] = getImageUrl($image_url);
				$listData['data'][$k]['diploma_photo'] = "<img style='height:100px;' src='".$listData['data'][$k]['diploma_photo']."'"." />";
				
				$listData['data'][$k]['DOACTION'] = '<a href="javascript:void(0)" onclick="admin.passCoach(\''.$v['id'].'\')">'."通过".'</a> - ';
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.DelCoachApply(\''.$v['id'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			}
		}elseif($type == "classList"){
			// 设置列表主键
			$this->_listpk = 'oid';
			// 取用户列表
			$listData = model('Coachorder')->getCoathorderList($limit,$map);
			//dump($listData);exit;
			// 数据格式化
			foreach ($listData['data'] as $k=>$v){
				$listData['data'][$k]['apply_time'] = date('Y-m-d H:i:s',$listData['data'][$k]['apply_time']);
				$listData['data'][$k]['apply_status'] = $listData['data'][$k]['apply_status']==0?"申请中":"已通过";
				$listData['data'][$k]['sex'] = $listData['data'][$k]['sex']==0?"女":"男";
				$uid = $listData['data'][$k]['uid'];
				$listData['data'][$k]['coach_id'] = M("Coach")->where("uid = ".$listData['data'][$k]['coach_id'])->find()["realname"];
			
				$listData['data'][$k]['DOACTION'] = '<a href="'.U('admin/Coach/editClass',array('oid'=>$v['oid'])).'">'."编辑".'</a> - ';
				
				$listData['data'][$k]['DOACTION'] .= '<a href="javascript:void(0)" onclick="admin.trueDelclass(\''.$v['oid'].'\')">'.L('PUBLIC_REMOVE_COMPLETELY').'</a>';
			}
	
		
		}
		return $listData;
	}
	//编辑教练
	public function editCoach(){
		//dump($_REQUEST);exit;
		
		$this->assign('pageTitle','编辑教练');
		
		$this->pageKeyList = array("realname","level","sex","birth","sport","photo","diploma_photo");
		$this->opt['sex'] = array('1'=>'男','0'=>'女');
		$this->opt['level'] = array('2'=>"高级",'1'=>'中级','0'=>'初级');
		//$this->opt['essence'] = array('1'=>'是','0'=>'否');
		$coach = model('Coach')->where('id='.$_GET['id'])->find();
		if($coach['photo']){
			$image = D('attach')->where('attach_id='.$coach['photo'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$coach['photo_url'] = getImageUrl($image_url);
		}
		if($coach['diploma_photo']){
			$image = D('attach')->where('attach_id='.$coach['diploma_photo'])->find();
			$image_url = $image['save_path'].$image['save_name'];
			$coach['diploma_photo_url'] = getImageUrl($image_url);
		}
		$this->notEmpty = array('image');
		$this->savePostUrl = U('admin/Coach/doEditCoach',array("id"=>$_GET['id']));
		
		$this->displayConfig($coach);
	}
	
	public function doEditCoach(){
		//dump($_REQUEST);
		$id = t($_REQUEST["id"]);
		$coach = M("Coach");
		$coach->level = t($_REQUEST["level"]);
		$coach->sex = t($_REQUEST["sex"]);
		$coach->birth = t($_REQUEST["birth"]);
		$coach->sport = t($_REQUEST["sport"]);
		$coach->photo = t($_REQUEST["photo"]);
		$coach->realname = t($_REQUEST["realname"]);
		$coach->diploma_photo = t($_REQUEST["diploma_photo"]);
		if($coach->where("id = $id")->save()){
			$this->assign('jumpUrl', U('admin/Coach/index'));
			$this->success(L('PUBLIC_SYSTEM_MODIFY_SUCCESS'));
		}else{
			$this->error($coach->getError());
		}
				
		
	}
	public function addCalssToCoach(){
		//dump($_REQUEST);exit;
		$id = t($_REQUEST["id"]);
		$name = M("Coach")->where("id = $id")->find()["realname"];
		$this->assign('pageTitle','添加课程   </br> <small>'.$name."  教练</small>");
		
		$this->pageKeyList = array('title','content',"people_limit","start_time");
		$this->opt['sex'] = array('1'=>'男','0'=>'女');
		//$this->opt['essence'] = array('1'=>'是','0'=>'否');
		$this->notEmpty = array('title','content',"people_limit","start_time");
		$this->savePostUrl = U('admin/Coach/doAddClass',array("id"=>$_GET['id'],"uid"=>$_REQUEST["uid"]));
		
		$this->displayConfig();
	}
	
	public function doAddClass(){
		
		$coach_order = M("Coach_order");
		$coach_order->coach_id = t($_REQUEST["uid"]);
		$coach_order->title = t($_REQUEST["title"]);
		$coach_order->content = t($_REQUEST["content"]);
		$coach_order->people_limit = t($_REQUEST["people_limit"]);
		$coach_order->start_time = t($_REQUEST["start_time"]);
		$coach_order->status = 0;
		if($coach_order->add()){
			$this->assign('jumpUrl', U('admin/Coach/index'));
			$this->success("添加成功");
		}else{
			$this->error($coach_order->getError());		
		}
		
	}
	
	public function doBanCoach(){
		$id = t($_REQUEST["id"]);
		$coach = M("Coach");
		$coach->apply_status = -1;
		if ($coach->where("id = $id")->save()){
			
			$return['status'] = 1;
			$return['data'] = "禁用成功";
		}else{
				$return['status'] = 0;
				$return['data']   = "操作失败";
		}
			echo json_encode($return);exit();
	}
	
	public function addCoach(){
		
		$this->assign('pageTitle','添加教练 ');
		$_REQUEST['tabHash'] = 'addCoach';
		// 初始化约单列表管理菜单
		$this->_initCoachListAdminMenu('addCoach');
		
		$this->pageKeyList = array('uid','level',"sex","birth","realname","sport","photo","diploma_photo","apply_status");
// 		$this->opt['uid'] = M("User")->where("is_coach = 0")->select();
		$this->opt['apply_status'] = array('1'=>'通过','0'=>'待审核');
		$this->notEmpty = array('uid','level',"sex","birth","realname","sport","photo","diploma_photo","apply_status");
		$this->savePostUrl = U('admin/Coach/doAddCoach',array("id"=>$_GET['id']));
		$this->onsubmit = 'admin.addCoachSubmitCheck(this)';
		$this->displayConfig();
	}
	//添加教练
	public function doAddCoach(){
		if(!$this->doCheckUser($_REQUEST["uid"])){
			$this->error("不能选择已经是教练的用户！");
		}
		//dump($_REQUEST);
		$coach = M("Coach");
		$coach->uid = t($_REQUEST["uid"]);
		$coach->level = t($_REQUEST["level"]);
		$coach->realname = t($_REQUEST["realname"]);
		$coach->sport = t($_REQUEST["sport"]);
		$coach->photo = t($_REQUEST["photo"]);
		$coach->diploma_photo = t($_REQUEST["diploma_photo"]);
		$coach->birth = t($_REQUEST["birth"]);
		$coach->apply_status = t($_REQUEST["apply_status"]);
		$coach->apply_time = time();
		if($coach->add()){
			if($_REQUEST["apply_status"] == 1){
				$user = M("User");
				$user->is_coach = 1;
				$user->where("uid = ".$_REQUEST['uid'])->save();
				$this->CoachGroup($_REQUEST['uid'], "add");
			}
			$this->assign('jumpUrl', U('admin/Coach/index'));
			$this->success("添加成功");
			}else{
				$this->error($coach->getError());
			}
	}
	public function doRecoverCoach(){
		$id = t($_REQUEST["id"]);
		$coach = M("Coach");
		$coach->apply_status = 1;
		if ($coach->where("id = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "恢复成功";
		}else{
			$return['status'] = 0;
			$return['data']   = "操作失败";
		}
		echo json_encode($return);exit();
	}
	public function doTrueDelCoach(){
		$id = t($_REQUEST["id"]);
		$coach = M("Coach");
		$uid = $coach->where("id = $id")->find()["uid"];
		if ($coach->where("id = $id")->delete()){
			M("User")->where("uid =$uid")->delete();
			$return['status'] = 1;
			$return['data'] = "删除成功";
		}else{
			$return['status'] = 0;
			$return['data']   = "操作失败";
		}
		echo json_encode($return);exit();
	}
	public function doDelCoach(){
		$id = t($_REQUEST["id"]);
		$coach = M("Coach");
		$uid = $coach->where("id = $id")->find()["uid"];
		if ($coach->where("id = $id")->delete()){
			$this->changeUser($uid,0);
			$this->CoachGroup($uid, "dele");
			$return['status'] = 1;
			$return['data'] = "删除成功";
		}else{
			$return['status'] = 0;
			$return['data']   = "操作失败";
		}
		echo json_encode($return);exit();
	}
	//课程
	public function editClass(){
// 		dump($_REQUEST);exit;
		$this->assign('pageTitle','编辑课程');
		
		$this->pageKeyList = array('oid','title',"content","coach_id","people_limit","start_time","status");
		$this->opt['status'] = array('1'=>'进行中','0'=>'申请中');
		//$this->opt['essence'] = array('1'=>'是','0'=>'否');
		$class = M("Coach_order")->where("oid = ".$_REQUEST["oid"])->find();
		
		$this->notEmpty = array('id','title',"content","coach_id","people","start_time","status");
		$this->savePostUrl = U('admin/Coach/doEditClass',array("id"=>$_GET['id']));
		
		$this->displayConfig($class);
	}
	public function doEditClass(){

		$coach_order = M("Coach_order");
		$coach_order->coach_id = t($_REQUEST["uid"]);
		$coach_order->title = t($_REQUEST["title"]);
		$coach_order->content = t($_REQUEST["content"]);
		$coach_order->people_limit = t($_REQUEST["people_limit"]);
		$coach_order->start_time = t($_REQUEST["start_time"]);
		$coach_order->status = t($_REQUEST["status"]);
		if($coach_order->where("oid = ".$_REQUEST["oid"])->save()){
			$this->assign('jumpUrl', U('admin/Coach/classList'));
			$this->success("修改成功");
		}else{
			$this->error($coach_order->getError());
		}
	}
	public function doPassCoach(){
		
		$id = t($_REQUEST["id"]);
		$coach = M("Coach");
		$coach->apply_status = 1;
		
		if ($coach->where("id = $id")->save()){
			$uid = $coach->where("id = $id")->find()["uid"];
			$this->changeUser($uid);
			$this->CoachGroup($uid, "add");
			//
			$push = NEW PushAction();
			$push->pushOneDeviceLocal($id,"恭喜你！申请教练成功！");
			
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   = $coach->getDbError();
		}
		echo json_encode($return);exit();
	}
	
	public function doNotPassCoach(){
		$id = t($_REQUEST["id"]);
		$coach = M("Coach");
		$coach->apply_status = -2;
		if ($coach->where("id = $id")->save()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data']   = "操作失败";
		}
		echo json_encode($return);exit();
	}
	public function doDelCoachApply(){
		$coach = M("Coach");
		$id = $_REQUEST['id'];
		
		if($coach->where("id = $id")->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
	
		exit(json_encode($return));
	}
	public function doDelCoachApplys(){
		$coach = M("Coach");
		$id = $_REQUEST['id'];
		$str = "id in (";
				for($i=0;$i<count($id);$i++){
				$str .= $id[$i].',';
				}
				$str = trim($str,',');
				$str .= ")";
		if($coach->where($str)->delete()){
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
			$return['status'] = 0;
			$return['data'] = "操作失败";
		}
	
		exit(json_encode($return));
	}
	public function changeUser($uid,$is_coach=1){
		$user = M("User");
		$user->is_coach = $is_coach;
		if ($user->where("uid = $uid")->save()){
			return true;
		}else{
			return false;
		}
	}
	
	public function  doTrueDelclass(){
		$oid = t($_REQUEST["oid"]);
		//删除记录
		if(M("Coach_order")->where("oid = $oid")->delete()){
			M("Coach_order_data")->where("oid = $oid")->delete();
			$return['status'] = 1;
			$return['data'] = "操作成功";
		}else{
				$return['status'] = 0;
				$return['data'] = "操作失败";
		}
		exit(json_encode($return));
	}
	public function doCheckUser($uid){
		
		$is_coach = M("User")->where("uid = $uid")->find()["is_coach"];
		if($is_coach){
			//1
			return  false;
		}else{
			//0
			return  true;
		}
		
	}
	/**
	 * 教练操作时的修改用户组操作
	 * uid 要修改的用户ID
	 * type 操作的类型 add 添加教练属性  dele去除教练熟悉  
	 * 		教练组ID为  7
	 * */
	public function CoachGroup($uid,$type){
		$group_link = M("User_group_link");
		
		if($type == "add"){
			//添加教练资格
			$group_link->uid = $uid;
			$group_link->user_group_id = 7;
			if($group_link->add()){
				return true;
			}
		}elseif ($type == "dele"){
			//去除教练资格
			if($group_link->where("uid = $uid and user_group_id = 7")->delete()){
				return true;
			}
			
		}
		return false;
	
	}
}